package com.yanfan.maintain.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.exception.base.BaseException;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.energy.domain.*;
import com.yanfan.energy.domain.enums.DeviceManageEnums;
import com.yanfan.energy.domain.enums.WorkOrderStatus;
import com.yanfan.maintain.mapper.EquipmentRepairDao;
import com.yanfan.maintain.mapper.ImageUrlDao;
import com.yanfan.maintain.service.EquipmentRepairService;
import com.yanfan.maintain.service.MaintenancePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * 设备维修表(EquipmentRepair)表服务实现类
 *
 * @author makejava
 * @since 2023-03-13 17:55:00
 */
@Service("equipmentRepairService")
@Transactional(rollbackFor = Exception.class)
public class EquipmentRepairServiceImpl implements EquipmentRepairService {
    @Autowired
    private EquipmentRepairDao equipmentRepairDao;
    @Autowired
    private ImageUrlDao imageUrlDao;
    @Autowired
    private MaintenancePlanService maintenancePlanService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EquipmentRepair queryById(Long id) {
        EquipmentRepair equipmentRepair = this.equipmentRepairDao.queryById(id);
        if (equipmentRepair != null) {
            ImageUrl imageUrl = new ImageUrl();
            imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
            imageUrl.setOrderId(id);
            imageUrl.setBusType(1);
            equipmentRepair.setImageUrlList(imageUrlDao.queryAll(imageUrl));
            imageUrl.setBusType(2);
            equipmentRepair.setConfirmImages(imageUrlDao.queryAll(imageUrl));
        }
        return equipmentRepair;
    }

    /**
     * 分页查询
     *
     * @param equipmentRepair 筛选条件
     * @param
     * @return 查询结果
     */
    @Override
    public List<EquipmentRepair> queryByPage(EquipmentRepair equipmentRepair) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // if (!user.isAdmin()) {
        //     equipmentRepair.setUserId(user.getUserId());
        // }
        return this.equipmentRepairDao.queryAllByLimit(equipmentRepair);
    }

    @Override
    public List<EquipmentRepair> listByztGuid(EquipmentRepair equipmentRepair) {
        return equipmentRepairDao.listByztGuid(equipmentRepair);
    }

    /**
     * 新增数据
     *
     * @param equipmentRepair 实例对象
     * @return 实例对象
     */
    @Override
    public EquipmentRepair insert(EquipmentRepair equipmentRepair) {
        equipmentRepair.setStatus(Math.toIntExact(WorkOrderStatus.SUBMITTED.getCode()));// 默认状态为待处理
        if (equipmentRepair.getEquipmentId() == null) {
            throw new BaseException("equipmentId  设备Id不能为空");
        }
        /*if (equipmentRepair.getRepairPerson() == null) {
            throw new BaseException("repairPerson  维修人不能为空");
        }*/

        equipmentRepair.setCreateTime(new Date());
        this.equipmentRepairDao.insert(equipmentRepair);
        List<SpareParts> sparePartsList = equipmentRepair.getSparePartsList();
        List<SpareParts> addList = new ArrayList<>(); // 要加入的集合数据
        // 处理前端 传非空List ，但 零备件id 和零备件数量为空的情况
        if (!CollectionUtils.isEmpty(sparePartsList)) {
            for (SpareParts spareParts : sparePartsList) {
                if (spareParts.getStockQuantity() != null && spareParts.getId() != null) {
                    addList.add(spareParts);
                }
            }
        }
        // 新插入零备件
        if (!CollectionUtils.isEmpty(addList)) {
            equipmentRepairDao.insertBatchSpareParts(addList, equipmentRepair.getId());
        }

        ImageUrl imgs = new ImageUrl();
        imgs.setOrderId(equipmentRepair.getId());
        imgs.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());

        if (Objects.nonNull(equipmentRepair.getImageUrlList())) {
            imgs.setBusType(1);
            imageUrlDao.deleteByOrderIdType(imgs);
            for (ImageUrl imageUrl : equipmentRepair.getImageUrlList()) {
                if (!StrUtil.isEmpty(imageUrl.getImgUrl())) {
                    imageUrl.setOrderId(equipmentRepair.getId());
                    imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
                    imageUrl.setBusType(1);
                }
            }
            if (!CollectionUtils.isEmpty(equipmentRepair.getImageUrlList())) {
                imageUrlDao.insertBatch(equipmentRepair.getImageUrlList());
            }
        }
        return equipmentRepair;
    }

    /**
     * 修改数据
     *
     * @param equipmentRepair 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public EquipmentRepair update(EquipmentRepair equipmentRepair) {
        if (equipmentRepair.getEquipmentId() == null) {
            throw new BaseException("equipmentId  设备Id不能为空");
        }
        if (equipmentRepair.getId() == null) {
            throw new BaseException("id  维修ID不能为空");
        }
        /*if (equipmentRepair.getRepairPerson() == null) {
            throw new BaseException("repairPerson  维修人不能为空");
        }*/
        // if (CollectionUtils.isEmpty(equipmentRepair.getSparePartsList())) {
        //     throw new BaseException("sparePartsList  备品备件列表不能为空");
        // }
        equipmentRepair.setCreateId(null);
        equipmentRepair.setCreateTime(null);
        this.equipmentRepairDao.update(equipmentRepair);
        // 先删除 后新增
        equipmentRepairDao.deleteRepairPartsByRepairId(equipmentRepair.getId());
        // 新插入零备件
        if (!CollectionUtils.isEmpty(equipmentRepair.getSparePartsList())) {
            equipmentRepairDao.insertBatchSpareParts(equipmentRepair.getSparePartsList(), equipmentRepair.getId());
        }


        ImageUrl imgs = new ImageUrl();
        imgs.setOrderId(equipmentRepair.getId());
        imgs.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());

        if (Objects.nonNull(equipmentRepair.getImageUrlList())) {
            imgs.setBusType(1);
            imageUrlDao.deleteByOrderIdType(imgs);
            for (ImageUrl imageUrl : equipmentRepair.getImageUrlList()) {
                if (!StrUtil.isEmpty(imageUrl.getImgUrl())) {
                    imageUrl.setOrderId(equipmentRepair.getId());
                    imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
                    imageUrl.setBusType(1);
                }
            }
            if (!CollectionUtils.isEmpty(equipmentRepair.getImageUrlList())) {
                imageUrlDao.insertBatch(equipmentRepair.getImageUrlList());
            }
        }


        if (Objects.nonNull(equipmentRepair.getConfirmImages())) {
            imgs.setBusType(2);
            imageUrlDao.deleteByOrderIdType(imgs);
            for (ImageUrl imageUrl : equipmentRepair.getConfirmImages()) {
                if (!StrUtil.isEmpty(imageUrl.getImgUrl())) {
                    imageUrl.setOrderId(equipmentRepair.getId());
                    imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
                    imageUrl.setBusType(2);
                }
            }
            if (!CollectionUtils.isEmpty(equipmentRepair.getConfirmImages())) {
                imageUrlDao.insertBatch(equipmentRepair.getConfirmImages());
            }
        }
        return this.queryById(equipmentRepair.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        equipmentRepairDao.deleteRepairPartsByRepairId(id);
        return this.equipmentRepairDao.deleteById(id) > 0;
    }

    /**
     * 根据设备Id 查询返回设备的相关信息
     *
     * @param id
     * @return
     */
    @Override
    public DeviceVo deviceDetailById(Long id) {
        DeviceVo deviceVo = equipmentRepairDao.deviceDetailById(id);
        return deviceVo;
    }

    @Override
    public List<BaseOrderEntity> orderList(BaseDictEntity baseDictEntity) {
        List<BaseOrderEntity> list = new ArrayList<>();
        if (baseDictEntity.getType() == null) {
            BaseOrderEntity base = new BaseOrderEntity();
            base.setId(1L);
            base.setName("维修");
            BaseOrderEntity base2 = new BaseOrderEntity();
            base2.setId(2L);
            base2.setName("巡检");
            BaseOrderEntity base3 = new BaseOrderEntity();
            base3.setId(3L);
            base3.setName("保养");
            list.add(base);
            list.add(base2);
            list.add(base3);
            return list;
        }

        /**
         * type  :  1  维修 ，2  巡检 3  保养
         */
        return equipmentRepairDao.orderList(baseDictEntity);
    }

    @Override
    public List<BaseOrderEntity> deviceTypeList(BaseDictEntity baseDictEntity) {

        return equipmentRepairDao.deviceTypeList(baseDictEntity);
    }

    @Override
    public List<BaseOrderEntity> brandList(BaseDictEntity baseDictEntity) {
        return equipmentRepairDao.brandList(baseDictEntity);
    }

    @Override
    public int updateRepairImgs(EquipmentRepair repair) {
        ImageUrl imgs = new ImageUrl();
        imgs.setOrderId(repair.getId());
        imgs.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
        imgs.setBusType(1);

        if (Objects.nonNull(repair.getImageUrlList())) {
            int di = imageUrlDao.deleteByOrderIdType(imgs);
            if (!CollectionUtils.isEmpty(repair.getImageUrlList())) {
                for (ImageUrl imageUrl : repair.getImageUrlList()) {
                    if (!StrUtil.isEmpty(imageUrl.getImgUrl())) {
                        imageUrl.setOrderId(repair.getId());
                        imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
                        imageUrl.setBusType(1);
                    }
                }
                return imageUrlDao.insertBatch(repair.getImageUrlList());
            } else {
                return di;
            }
        }
        return 0;
    }

    @Override
    public List<ImageUrl> selectRepairImgs(EquipmentRepair repair) {
        ImageUrl imageUrl = new ImageUrl();
        imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
        imageUrl.setOrderId(repair.getId());
        imageUrl.setBusType(1);
        return imageUrlDao.queryAll(imageUrl);
    }

    @Override
    public int updateConfirmRepairImgs(EquipmentRepair repair) {
        ImageUrl imgs = new ImageUrl();
        imgs.setOrderId(repair.getId());
        imgs.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
        imgs.setBusType(2);

        if (Objects.nonNull(repair.getImageUrlList())) {
            int di = imageUrlDao.deleteByOrderIdType(imgs);
            if (!CollectionUtils.isEmpty(repair.getImageUrlList())) {
                for (ImageUrl imageUrl : repair.getImageUrlList()) {
                    if (!StrUtil.isEmpty(imageUrl.getImgUrl())) {
                        imageUrl.setOrderId(repair.getId());
                        imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
                        imageUrl.setBusType(2);
                    }
                }
                return imageUrlDao.insertBatch(repair.getImageUrlList());
            } else {
                return di;
            }
        }
        return 1;
    }

    @Override
    public List<ImageUrl> selectConfirmRepairImgs(EquipmentRepair repair) {
        ImageUrl imageUrl = new ImageUrl();
        imageUrl.setType(DeviceManageEnums.ImageType.DEVICE_REPAIR.getCode());
        imageUrl.setOrderId(repair.getId());
        imageUrl.setBusType(2);
        return imageUrlDao.queryAll(imageUrl);
    }
}
